package org.jmc.refmanager.web.wicket.model;

import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.jmc.refmanager.domain.model.OrgUnit;
import org.jmc.refmanager.domain.service.OrgUnitManager;

/**
 * 
 *
 */
public class ServiceModel extends LoadableDetachableModel<OrgUnit> {
	private static final long serialVersionUID = 1L;

	@SpringBean(name="orgUnitManager")
	private OrgUnitManager orgUnitManager;
	
//	private transient OrgUnit service;
	private Integer serviceId;
	
	
	/**
	 * 
	 */
	public ServiceModel(OrgUnit service) {
		super();
		InjectorHolder.getInjector().inject(this);
		this.serviceId = service.getId();
//		this.service = service;
	}

	public ServiceModel(Integer serviceId) {
		super();
		InjectorHolder.getInjector().inject(this);
		this.serviceId = serviceId;
	}

	@Override
	protected OrgUnit load() {
		if (this.serviceId == null || this.serviceId == 0) {
			return new OrgUnit();
		} else {
//		if (this.service == null && this.serviceId != null) {
//			this.service = orgUnitManager.findById(serviceId);
//		}
			return this.orgUnitManager.findById(this.serviceId);
		}
	}
	
}
